package com.relsell.utility;

import java.io.File;
import java.util.ArrayList;

import android.app.ActionBar;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.relsell.controller.DataInterface;
import com.relsell.controller.InstructorCallBack;
import com.relsell.controller.RecentBollywoodLessonsCallBack;
import com.relsell.controller.RecentSongLessonsCallBack;
import com.relsell.controller.SlideMainActivityCallback;
import com.relsell.tmcprototype.R;
import com.relsell.view.phone.Instructors;
import com.relsell.view.phone.RecentBollywoodSongs;
import com.relsell.view.phone.RecentSongLessons;

/**
 * Application Var class used for Application Wide Variables Only a single
 * Instance of this class will be available.
 * 
 * @author admin
 * 
 */
public class ApplicationVars {
	private static ApplicationVars singleInstance;
	private boolean mLogging;
	public static String LOG_TAG = "TCMPrototype";
	private static ArrayList<DataInterface> mGuitarDataList;
	private static ArrayList<DataInterface> mInstructorsDataList;
	private boolean mInstructorDataNeededfromServer;
	private boolean mRecentSongsLessonDataNeededfromServer;
	private boolean mRecentBollywoodLessonDataNeededfromServer;
	private InstructorCallBack emptyInstructorCallBack;
	private SlideMainActivityCallback emptySliderCallBack;
	private RecentSongLessonsCallBack emptyRecentSongLessonsCallBack;
	private RecentBollywoodLessonsCallBack emptyRecentBollywoodLessonsCallBack;
	private ArrayList<DataInterface> mRecentSongLessonsList;
	private ArrayList<DataInterface> mRecentBollyWoodSongsList;
	private boolean isTestingUrl;
	
	
	
	
	private Handler mHandler = new Handler () {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case Instructors.GOTDATAFORINSTRUCTORS:
				emptyInstructorCallBack.gotInstructorListData(true);
				break;
			case RecentSongLessons.GOT_DATA_FOR_RECENT_SONG_LESSON:
				emptyRecentSongLessonsCallBack.gotRecentSongLessonsListData(true);
				break;
			case RecentBollywoodSongs.GOT_DATA_FOR_RECENT_BOLLYWOOD_LESSON:
				emptyRecentBollywoodLessonsCallBack.gotBollywoodLessonsListData(true);
				break;	
			}
		}
	};
	

	private ApplicationVars() {
		setLogging(true); // when in production mode setLogging = false;
		setTestingUrl(true);
	}

	/**
	 * @return single and only instance of this class
	 */
	public static ApplicationVars getInstance() {
		if (singleInstance == null) {
			singleInstance = new ApplicationVars();
		}
		return singleInstance;
	}

	public boolean isLogging() {
		return mLogging;
	}

	public void setLogging(boolean mLogging) {
		this.mLogging = mLogging;
	}

	public void logError(String toLog) {
		if(isLogging()) {
		   Log.v("", " " + toLog);
		}
	}

	public ArrayList<DataInterface> getGuitarDataList() {
		return mGuitarDataList;
	}

	public void setGuitarDataList(ArrayList<DataInterface> mGuitarDataList) {
		this.mGuitarDataList = mGuitarDataList;
	}

	public ArrayList<DataInterface> getInstructorsDataList() {
		return mInstructorsDataList;
	}

	public void setInstructorsDataList(
			ArrayList<DataInterface> mInstructorsDataList) {
		this.mInstructorsDataList = mInstructorsDataList;
	}

	public void showCustomDialog(Context context) {
		// custom dialog
		View v = ((LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.custom_dialog, null);
		Button btn1 = (Button)v.findViewById(R.id.button1);
		
		final AlertDialog dialog = new AlertDialog.Builder(context).setView(v)
				.create();

		WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();

		wmlp.gravity = Gravity.TOP | Gravity.LEFT;
		wmlp.x = 0; // x position
		wmlp.y = 0; // y position
		wmlp.width = WindowManager.LayoutParams.MATCH_PARENT;
		dialog.show();
		dialog.getWindow().setAttributes(wmlp);
		btn1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				
			}
		});
	}

	public void setupActionBar(Context context, ActionBar actionBar,
			boolean displayShowTitleEnabled, boolean displayUseLogoEnabled,
			boolean displayHomeAsUpEnabled, boolean displayShowCustomEnabled,
			boolean displayShowHomeEnabled) {
		actionBar.setDisplayShowTitleEnabled(displayShowTitleEnabled);
		actionBar.setDisplayUseLogoEnabled(displayUseLogoEnabled);
		actionBar.setDisplayHomeAsUpEnabled(displayHomeAsUpEnabled);
		actionBar.setDisplayShowCustomEnabled(displayShowCustomEnabled);
		actionBar.setDisplayShowHomeEnabled(displayShowHomeEnabled);

		View customNav = ((LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.cutomizedactionbar, null); // layout which contains
													// your button.
		TextView tv = (TextView) customNav.findViewById(R.id.textView1);
		tv.setText(Html.fromHtml(context.getString(R.string.actionbar_name)));
		actionBar.setCustomView(customNav);
	}

	public boolean isInstructorDataNeededfromServer() {
		String fileName = "page0_childern1_torrins.json";
		setInstructorDataNeededfromServer(setcheckFileAvailabilityAndEmptyness(fileName));
		return mInstructorDataNeededfromServer;
	}
	

	private boolean setcheckFileAvailabilityAndEmptyness(String fileName) {
		boolean result = false;
		File folderDir = null;
		folderDir = new File(Environment.getExternalStorageDirectory()
				+ "/torrins_data");
		if (folderDir.isDirectory()) {
			File file = new File(folderDir, fileName);
			if (!file.exists()) {
				result = false;
			}
		} else {
			result = true;
		}
		return result;
	}

	public void setInstructorDataNeededfromServer(
			boolean mInstructorDataNeededfromServer) {
		this.mInstructorDataNeededfromServer = mInstructorDataNeededfromServer;
	}

	public Handler getHandler() {
		return mHandler;
	}

	public void setHandler(Handler mHandler) {
		this.mHandler = mHandler;
	}

	public InstructorCallBack getEmptyInstructorCallBack() {
		return emptyInstructorCallBack;
	}

	public void setEmptyInstructorCallBack(InstructorCallBack emptyInstructorCallBack) {
		this.emptyInstructorCallBack = emptyInstructorCallBack;
	}

	public SlideMainActivityCallback getEmptySliderCallBack() {
		return emptySliderCallBack;
	}

	public void setEmptySliderCallBack(SlideMainActivityCallback emptySliderCallBack) {
		this.emptySliderCallBack = emptySliderCallBack;
	}

	public ArrayList<DataInterface> getRecentSongLessonsList() {
		return mRecentSongLessonsList;
	}

	public void setRecentSongLessonsList(ArrayList<DataInterface> mRecentSongLessonsList) {
		this.mRecentSongLessonsList = mRecentSongLessonsList;
	}

	public RecentSongLessonsCallBack getEmptyRecentSongLessonsCallBack() {
		return emptyRecentSongLessonsCallBack;
	}

	public void setEmptyRecentSongLessonsCallBack(
			RecentSongLessonsCallBack emptyRecentSongLessonsCallBack) {
		this.emptyRecentSongLessonsCallBack = emptyRecentSongLessonsCallBack;
	}

	public boolean isRecentSongsLessonDataNeededfromServer() {
		String fileName = "page4_torrins_recent_songs_lesson.json";
		setRecentSongsLessonDataNeededfromServer(setcheckFileAvailabilityAndEmptyness(fileName));
		return mRecentSongsLessonDataNeededfromServer;
	}
	public boolean isRecentBollyWoodLessonDataNeededfromServer() {
		String fileName = "page7_torrins_recent_songs_lesson.json";
		setRecentBollywoodLessonDataNeededfromServer(setcheckFileAvailabilityAndEmptyness(fileName));
		return mRecentBollywoodLessonDataNeededfromServer;
	}

	public void setRecentBollywoodLessonDataNeededfromServer(
			boolean mRecentBollywoodLessonDataNeededfromServer) {
		this.mRecentBollywoodLessonDataNeededfromServer = mRecentBollywoodLessonDataNeededfromServer;
	}
	public void setRecentSongsLessonDataNeededfromServer(
			boolean mRecentSongsLessonDataNeededfromServer) {
		this.mRecentSongsLessonDataNeededfromServer = mRecentSongsLessonDataNeededfromServer;
	}
    public void startTestmonialsAnimation(TextView status,TextView info) {
    	Animation animationToLeft = new TranslateAnimation(0, 0, 50, -50);
		animationToLeft.setDuration(4000); 
		animationToLeft.setRepeatMode(Animation.RESTART);
		animationToLeft.setRepeatCount(Animation.INFINITE);
		status.setAnimation(animationToLeft);
		info.setAnimation(animationToLeft);
    }

	public boolean isTestingUrl() {
		return isTestingUrl;
	}

	public void setTestingUrl(boolean isTestingUrl) {
		this.isTestingUrl = isTestingUrl;
	}

	public ArrayList<DataInterface> getRecentBollyWoodSongsList() {
		return mRecentBollyWoodSongsList;
	}

	public void setRecentBollyWoodSongsList(ArrayList<DataInterface> mRecentBollyWoodSongsList) {
		this.mRecentBollyWoodSongsList = mRecentBollyWoodSongsList;
	}

	public RecentBollywoodLessonsCallBack getEmptyRecentBollywoodLessonsCallBack() {
		return emptyRecentBollywoodLessonsCallBack;
	}

	public void setEmptyRecentBollywoodLessonsCallBack(
			RecentBollywoodLessonsCallBack emptyRecentBollywoodLessonsCallBack) {
		this.emptyRecentBollywoodLessonsCallBack = emptyRecentBollywoodLessonsCallBack;
	}
}
